package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.mapper.AdminMapper;
import com.ruan.mapper.DepartmentMapper;
import com.ruan.mapper.EmployeeMapper;
import com.ruan.mapper.SalaryMapper;
import com.ruan.pojo.*;
import com.ruan.service.SalaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
@Slf4j
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements SalaryService {

    @Resource
    private SalaryMapper salaryMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private DepartmentMapper departmentMapper;


    /**
     * 工资发放
     * @param id 薪资详情id
     * @param adminId 管理员id
     */
    @Override
    public void payWages(Integer id, Integer adminId) {
        Admin admin = adminMapper.selectById(adminId);
        Salary salary = salaryMapper.selectById(id);
        salary.setPayTime(new java.util.Date());
        salary.setApprover(admin.getUsername());
        salary.setStatus("已发放");
//        System.out.println("员工工资状态："+salary);
        salaryMapper.updateById(salary);
    }

    @Override
    public IPage<Salary> getSelfSalary(int currentPage,
                                       int pageSize,
                                       QuerySelfAttendanceCondition querySelfAttendanceCondition) {
        Date month = querySelfAttendanceCondition.getMonth();//2024 -02-01
        IPage<Salary> iPage=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Salary> queryWrapper = new LambdaQueryWrapper<>();
        //-----------------------------------
        LambdaQueryWrapper<Employee> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Employee::getPhone,querySelfAttendanceCondition.getPhone());
        Employee employee = employeeMapper.selectOne(queryWrapper1);

        queryWrapper.eq(Salary::getEmployeeId,employee.getId());
        if (month!=null){
            String[] split = month.toString().split("-");
            String date = split[0]+"-"+split[1];
            queryWrapper.eq(Salary::getMonth,date);
            return salaryMapper.selectPage(iPage,queryWrapper);
        }else {
            return salaryMapper.selectPage(iPage,queryWrapper);
        }
    }

}
